package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.core.Result;
import com.dongdongshop.core.ResultEnum;
import com.dongdongshop.pojo.Vo.SpecificationOptionVo;
import com.dongdongshop.pojo.seller.TbSpecification;
import com.dongdongshop.pojo.seller.TbSpecificationOption;
import com.dongdongshop.service.SpecifiCationService;
import com.dongdongshop.util.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("specification")
public class SpecificationController {

    @Reference
    private SpecifiCationService specifiCationService;

    private Logger logger = LoggerFactory.getLogger(SpecificationController.class);

    @RequestMapping("toList")
    public String toList(){
        return "admin/specification";
    }

    @RequestMapping("findSpecifiAll")
    @ResponseBody
    public Result findSpecifiAll(String specName,@RequestParam(defaultValue = "1")Integer pageNumber, @RequestParam(defaultValue = "5")Integer pageSize){
        try {
            PageBean<TbSpecification> pageResult = specifiCationService.findSpecifiAll(specName,pageNumber,pageSize);
            return Result.response(ResultEnum.SUCCESS).setData(pageResult);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.response(ResultEnum.ERROR);
        }
    }

    @RequestMapping("addSpecifiByName")
    @ResponseBody
    public Result addSpecifiByName(TbSpecification specification,Integer[] orders,String[] optionName){
        try {
            specifiCationService.addSpecifiByName(specification,orders,optionName);
            return Result.response(ResultEnum.SUCCESS);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.response(ResultEnum.ERROR);
        }
    }

    //批量删除
    @RequestMapping("deleteByIds")
    @ResponseBody
    public Result deleteByIds(@RequestParam("ids[]")Integer[] ids){

        try {
            int i = specifiCationService.deleteInfo(ids);
            return Result.response(ResultEnum.SUCCESS).setData(i);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.response(ResultEnum.ERROR);
        }

    }


    //回显规格管理信息
    @RequestMapping("toUpdate")
    @ResponseBody
    public Result toUpdate(Long id){
        try {
            List<SpecificationOptionVo> list =  specifiCationService.selectById(id);

            return Result.response(ResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.response(ResultEnum.ERROR);
        }
    }


    @RequestMapping("updateInfo")
    @ResponseBody
    public Result updateInfo(TbSpecification specification,Integer[] orders,String[] optionName){
        try {
            //调用service接口
            specifiCationService.updateInfo(specification,optionName,orders);
            return Result.response(ResultEnum.SUCCESS);
        }catch (Exception e){
            logger.error("修改操作: {}",e);
            return Result.response(ResultEnum.ERROR);
        }
    }
}
